package com.tju.ocean_ecology_website.service.impl;

import com.tju.ocean_ecology_website.entity.Activity;
import com.tju.ocean_ecology_website.entity.Team;
import com.tju.ocean_ecology_website.dao.TeamDao;
import com.tju.ocean_ecology_website.entity.TeamMember;
import com.tju.ocean_ecology_website.service.TeamService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * 团队表(Team)表服务实现类
 *
 * @author shaolong
 * @since 2025-04-29 19:34:06
 */
@Service("teamService")
public class TeamServiceImpl implements TeamService {
    @Resource
    private TeamDao teamDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Team queryById(Long id) {
        return this.teamDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param team 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Team> queryByPage(Team team, PageRequest pageRequest) {
        long total = this.teamDao.count(team);
        return new PageImpl<>(this.teamDao.queryAllByLimit(team, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param team 实例对象
     * @return 实例对象
     */
    @Override
    public Team insert(Team team) {
        this.teamDao.insert(team);
        return team;
    }

    /**
     * 修改数据
     *
     * @param team 实例对象
     * @return 实例对象
     */
    @Override
    public Team update(Team team) {
        this.teamDao.update(team);
        return this.queryById(team.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.teamDao.deleteById(id) > 0;
    }

    /**
     * 查询团队成员
     *
     * @param teamId 团队ID
     * @param pageRequest 分页对象
     * @return 团队成员列表
     */
    @Override
    public Page<TeamMember> queryTeamMembers(Long teamId, PageRequest pageRequest) {
        long total = this.teamDao.countTeamMembers(teamId);
        List<TeamMember> members = this.teamDao.queryTeamMembers(teamId, pageRequest);
        return new PageImpl<>(members, pageRequest, total);
    }

    /**
     * 查询团队活动
     *
     * @param teamId 团队ID
     * @param pageRequest 分页对象
     * @return 团队活动列表
     */
    @Override
    public Page<Activity> queryTeamActivities(Long teamId, PageRequest pageRequest) {
        long total = this.teamDao.countTeamActivities(teamId);
        List<Activity> activities = this.teamDao.queryTeamActivities(teamId, pageRequest);
        return new PageImpl<>(activities, pageRequest, total);
    }
}
